package com.betterjr.modules.productconfig;

import static com.betterjr.common.web.ControllerExceptionHandler.exec;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.betterjr.common.web.Servlets;

@Controller
@RequestMapping(value = "/Scf/ProductConfig")
public class ProductConfigController {

    private static final Logger logger = LoggerFactory.getLogger(ProductConfigController.class);

    @Reference(interfaceClass = IScfProductConfigService.class)
    private IScfProductConfigService productConfigService;

    @RequestMapping(value = "/addProductConfig", method = RequestMethod.POST)
    public @ResponseBody String addProductConfig(final HttpServletRequest request) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("保存融资产品信息,入参：" + anMap.toString());
        return exec(() -> productConfigService.webAddProductConfig(anMap), "融资产品信息保存失败", logger);
    }

    @RequestMapping(value = "/saveModifyProductConfig", method = RequestMethod.POST)
    public @ResponseBody String saveModifyProductConfig(final HttpServletRequest request, final Long id) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("融资产品信息查询,入参：" + anMap.toString());
        return exec(() -> productConfigService.webSaveModifyProductConfig(anMap, id), "修改融资产品信息失败", logger);
    }

    @RequestMapping(value = "/queryProductConfig", method = RequestMethod.POST)
    public @ResponseBody String queryProductConfig(final HttpServletRequest request, final int pageNum,
            final int pageSize, final int flag) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("分页查询产品列表,入参：" + anMap.toString());
        return exec(() -> productConfigService.webQueryProductConfig(anMap, flag, pageNum, pageSize), "融资产品信息查询失败",
                logger);
    }

    @RequestMapping(value = "/findProductConfig", method = RequestMethod.POST)
    public @ResponseBody String findProductConfig(final HttpServletRequest request, final String productCode) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("融资产品信息查询,入参：" + anMap.toString());
        return exec(() -> productConfigService.webFindProductConfig(productCode), "融资产品信息查询失败", logger);
    }

    @RequestMapping(value = "/findAssetDictAttachRelationList", method = RequestMethod.POST)
    public @ResponseBody String findAssetDictAttachRelationList(final HttpServletRequest request,
            final String productCode) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info(" 查询资产类型与附件关系列表,入参：" + anMap.toString());
        return exec(() -> productConfigService.webFindAssetDictAttachRelationList(anMap), "查询资产类型与附件关系列表失败", logger);
    }

    @RequestMapping(value = "/addAssetDictAttachRelation", method = RequestMethod.POST)
    public @ResponseBody String addAssetDictAttachRelation(final HttpServletRequest request) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("保存产品资产类型关联的附件类型关系" + anMap.toString());
        return exec(() -> productConfigService.webAddAssetDictAttachRelation(anMap), "保存产品资产类型关联的附件类型关系失败", logger);
    }

    @RequestMapping(value = "/addProductCoreRelation", method = RequestMethod.POST)
    public @ResponseBody String addProductCoreRelation(final HttpServletRequest request, final String productCode,
            final String custNo) {
        logger.info("保存产品与核心企业关系");
        return exec(() -> productConfigService.webAddProductCoreRelation(productCode, custNo), "保存产品与核心企业关系失败", logger);
    }

    @RequestMapping(value = "/addProductAssetDictRelation", method = RequestMethod.POST)
    public @ResponseBody String AddProductAssetDictRelation(final HttpServletRequest request, final String productCode,
            final String listIdType) {
        // Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("保存产品与资产类型关系表" + productCode);
        return exec(() -> productConfigService.webAddProductAssetDictRelation(productCode, listIdType),
                "保存产品与资产类型关系表失败", logger);
    }

    @RequestMapping(value = "/findAssetDictByProduct", method = RequestMethod.POST)
    public @ResponseBody String findAssetDictByProduct(final HttpServletRequest request, final String productCode) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("查询产品的附属品关系,入参：" + anMap.toString());
        return exec(() -> productConfigService.webFindAssetDictByProduct(productCode), "查询产品的附属品关系", logger);
    }

    @RequestMapping(value = "/queryCoreByProduct", method = RequestMethod.POST)
    public @ResponseBody String queryCoreByProduct(final HttpServletRequest request, final String productCode,
            final int pageNum, final int pageSize, final int flag) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("分页查询资产类型列表,入参：" + anMap.toString());
        return exec(() -> productConfigService.webQueryCoreByProduct(productCode, flag, pageNum, pageSize),
                "查询资产类型列表失败", logger);
    }

    @RequestMapping(value = "/findCoreByProductList", method = RequestMethod.POST)
    public @ResponseBody String findCoreByProductList(final HttpServletRequest request, final String productCode) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("查询核心企业列表,入参：" + anMap.toString());
        return exec(() -> productConfigService.webFindCoreByProductList(productCode), "查询核心企业列表失败", logger);
    }

    @RequestMapping(value = "/findAssetDict", method = RequestMethod.POST)
    public @ResponseBody String findAssetDict(final HttpServletRequest request, final Long factorNo) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("分页查询资产类型列表,入参：" + anMap.toString());
        return exec(() -> productConfigService.webFindAssetDict(factorNo), "查询资产类型列表失败", logger);
    }

    @RequestMapping(value = "/queryAssetDict", method = RequestMethod.POST)
    public @ResponseBody String queryAssetDict(final HttpServletRequest request, final Long factorNo, final int pageNum,
            final int pageSize, final int flag) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("分页查询资产类型列表,入参：" + anMap.toString());
        return exec(() -> productConfigService.webQueryAssetDict(factorNo, flag, pageNum, pageSize), "查询资产类型列表失败",
                logger);
    }

    @RequestMapping(value = "/delProductConfig", method = RequestMethod.POST)
    public @ResponseBody String delProductConfig(final HttpServletRequest request, final Long id) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("删除产品,入参：" + anMap.toString());
        return exec(() -> productConfigService.webDelProductConfig(id), "删除产品失败", logger);
    }

    @RequestMapping(value = "/queryProductKeyAndValue", method = RequestMethod.POST)
    public @ResponseBody String delProductConfig(final HttpServletRequest request, final Long coreCustNo,
            final Long factorNo) {
        Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("删除产品,入参：" + anMap.toString());
        return exec(() -> productConfigService.webQueryProductKeyAndValue(coreCustNo, factorNo), "查询产品失败", logger);
    }

    /**
     * 
     * @Title: queryFinancingProduct 
     * @Description: 分页查询状态为登记的融资产品
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @param flag 是否统计总数
     * @param factorNo 保理公司编号
     * @param isHandling true 经办员 false审核员
     * @return 登记的融资产品分页
     * @throws 
     * @author xuzx
     * @date 2017年11月20日 下午2:37:46
     */
    @RequestMapping(value = "/queryFinancingProduct", method = RequestMethod.POST)
    public @ResponseBody String queryFinancingProduct(final int pageNum, final int pageSize, final int flag,
            final Long factorNo, final boolean isHandling) {
        logger.info("查询登记融资产品,入参：factorNo=" + factorNo);
        return exec(() -> productConfigService.webQueryFinancingProduct(pageNum, pageSize, flag, factorNo, isHandling),
                "查询登记融资产品失败", logger);
    }

}
